package com.pvdnc.systemdebugger.userauth;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class TripleResultDialog extends Dialog {
    private static final String TAG="TripleResultDialog";

    private Handler mHandler;

    public static void ensureLooper(){
        if(Looper.myLooper()==null){
            Looper.prepare();
            Log.d(TAG,"looper has been prepared");
        }
    }

    private void resume(){
        Message m = mHandler.obtainMessage();
        mHandler.sendMessage(m);
    }

    private Context mContext;
    @SuppressLint("HandlerLeak")
    public TripleResultDialog(Context context) {
        super(context, R.style.TransparentDialog);
        ensureLooper();
        mHandler= new Handler() {
            @Override
            public void handleMessage(Message mesg) {
                // process incoming messages here
                //super.handleMessage(msg);
                throw new RuntimeException();
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
        setContentView(R.layout.activity_ipass_dialog);

        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        setCanceledOnTouchOutside(false);//点击屏幕不消失

        TextView lblTitle=(TextView)findViewById(R.id.lblTitle);
        lblTitle.setText(getTitle());
        TextView lblMessage=(TextView)findViewById(R.id.lblMessage);
        lblMessage.setText(getMessage());

        setButtonListener(R.id.cmdAllow,RESULT_ALLOW);
        setButtonListener(R.id.cmdDeny,RESULT_DENY);
        setButtonListener(R.id.cmdDenyAlways,RESULT_DENY_ALWAYS);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private String title;
    private String message;

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    private volatile int result;

    public static final int RESULT_ALLOW=1;
    public static final int RESULT_DENY=0;
    public static final int RESULT_DENY_ALWAYS=-1;

    private void setButtonListener(final int resId, final int resultCode)
            throws NullPointerException {
        Button button = (Button) findViewById(resId);
        if (button == null)
            throw new NullPointerException(resId + " cannot be found as a button");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                setResult(resultCode);
                Log.d(TAG,"dialog result:"+resultCode);
                resume();
            }
        });
    }

    public int showDialog(){
        this.show();
        //阻塞直到确认
        try {
            Looper.getMainLooper().loop();
        }
        catch(RuntimeException e2)
        {
            Log.d(TAG,"recover from waiting loop");
        }
        return getResult();
    }
}
